﻿''' <summary>
''' Form thực hiện việc đăng nhập
''' </summary>
''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
Public Class frmLogin

    ''' <summary>
    ''' Thuộc tính lưu đối tượng xử lý các thao tác nghiệp vụ
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Private m_oBusinessObject As BL.clsBLBase
    Public Property BusinessObject() As BL.clsBLBase
        Get
            Return m_oBusinessObject
        End Get
        Set(ByVal value As BL.clsBLBase)
            m_oBusinessObject = value
        End Set
    End Property

    ''' <summary>
    ''' Thuộc tính lưu lại tình trạng đăng nhập
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private m_eLogin As clscommon.LoginStatus = clscommon.LoginStatus.NotLoggedIn
    Public Property LoginStatus() As clscommon.LoginStatus
        Get
            Return m_eLogin
        End Get
        Set(ByVal value As clscommon.LoginStatus)
            m_eLogin = value
        End Set
    End Property

    ''' <summary>
    ''' Sub xóa các textbox trong form
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 09/07/2012</remarks>
    Private Sub ClearForm()

    End Sub

    ''' <summary>
    ''' Khi form login được load thì thực hiện việc kiểm tra connection string
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cboServer.SelectedIndex = 0
            If clscommon.CheckConnection(String.Format(My.Resources.ConnectionString, clscommon.DecodingData(My.Settings.Svrnm), clscommon.DecodingData(My.Settings.Svrlgnm), clscommon.DecodingData(My.Settings.Svrpsw))) = True Then
                grbServer.Visible = False
            Else
                grbServer.Visible = True
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi form login đóng thì kiểm tra xem người dùng đã đăng nhập thành công chưa
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private Sub frmLogin_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            If LoginStatus = clscommon.LoginStatus.NotLoggedIn Then
                Application.Exit()
            ElseIf LoginStatus = clscommon.LoginStatus.LoggedIn Then
                Me.Close()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thay đổi kích thước form khi grdServer ẩn/hiện
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 07/07/2012</remarks>
    Private Sub grbServer_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grbServer.VisibleChanged
        Try
            If grbServer.Visible = True Then
                Me.Size = New System.Drawing.Size(407, 388)
                cboServer.Text = clscommon.DecodingData(My.Settings.SvrType)
                txtServerName.Text = clscommon.DecodingData(My.Settings.Svrnm)
                txtServerLoginName.Text = clscommon.DecodingData(My.Settings.Svrlgnm)
                txtServerLoginPassword.Text = clscommon.DecodingData(My.Settings.Svrpsw)
                cboServer.Focus()
            Else
                Me.Size = New System.Drawing.Size(407, 223)
                txtUserName.Focus()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Hiển thị các thông tin truy cập server khi server được chọn
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 07/07/2012</remarks>
    Private Sub cboServer_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboServer.ValueChanged
        Try
            If cboServer.SelectedIndex = 0 Then
                txtServerName.Text = My.Resources.DefaultLocalServerName
                txtServerLoginName.Text = My.Resources.DefaultLocalLoginName
                txtServerLoginPassword.Text = My.Resources.DefaultLocalLoginPassword
            ElseIf cboServer.SelectedIndex = 1 Then
                txtServerName.Text = My.Resources.DefaultLanServerName
                txtServerLoginName.Text = My.Resources.DefaultLanLoginName
                txtServerLoginPassword.Text = My.Resources.DefaultLanLoginPassword
            ElseIf cboServer.SelectedIndex = 2 Then
                txtServerName.Text = My.Resources.DefaultOnlineServername
                txtServerLoginName.Text = My.Resources.DefaultOnlineLoginName
                txtServerLoginPassword.Text = My.Resources.DefaultOnlineLoginPassword
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi click vào nút database thì hiện khung để nhập thông tin server
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private Sub btnDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDatabase.Click
        Try
            If grbServer.Visible = False Then
                grbServer.Visible = True
            Else
                grbServer.Visible = False
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi click vào nút đăng nhập thì thực hiện việc đăng nhập
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Try
            With BusinessObject
                BusinessObject = New BL.clsBLBase(String.Format(My.Resources.ConnectionString, clscommon.DecodingData(My.Settings.Svrnm), clscommon.DecodingData(My.Settings.Svrlgnm), clscommon.DecodingData(My.Settings.Svrpsw)))
                Dim roleName As String = BusinessObject.CheckUser(clscommon.EncodingData(txtUserName.Text), clscommon.EncodingData(txtPassword.Text))
                If Not IsNothing(roleName) Then
                    My.Settings.Usrnm = clscommon.EncodingData2(txtUserName.Text)
                    My.Settings.Crrrl = clscommon.EncodingData2(roleName)
                    LoginStatus = clscommon.LoginStatus.LoggedIn
                    Me.Close()
                Else
                    clscommon.ShowException(My.Resources.LoginFail)
                    txtUserName.Focus()
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Click vào nút cancel thì thực hiện việc thoát phần mềm
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 06/07/2012</remarks>
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Application.Exit()
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi nhấn nút connect thì kiểm tra connection string, nếu kết nối thành công thì lưu connection string và file setting
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 07/07/2012</remarks>
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            If clscommon.CheckConnection(String.Format(My.Resources.ConnectionString, txtServerName.Text, txtServerLoginName.Text, txtServerLoginPassword.Text)) Then
                My.Settings.SvrType = clscommon.EncodingData2(cboServer.Text)
                My.Settings.Svrnm = clscommon.EncodingData2(txtServerName.Text)
                My.Settings.Svrlgnm = clscommon.EncodingData2(txtServerLoginName.Text)
                My.Settings.Svrpsw = clscommon.EncodingData2(txtServerLoginPassword.Text)
                grbServer.Visible = False
                clscommon.ShowInformation(My.Resources.ConnectSuccess)
                txtUserName.Focus()
            Else
                clscommon.ShowException(My.Resources.ConnectFail)
                cboServer.Focus()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
End Class